<?php
/**
* Mapping for an embedded ( scalar ) value
* 
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* 
* @copyright Copyright (c) 2008-2009 Ne8, LLC <josh.ribakoff@gmail.com>
* @license New BSD License
* @version 0.1
* @category Shuffler
* @package Shuffler_Mapper
* @subpackage Mapping
*/
class Shuffler_Mapper_Mapping_Embedded extends Shuffler_Mapper_Mapping
{
    protected $table;
    
    protected $fieldIsInline = true;
    
    /**
    * Get the class property name for this mapping, used to derive name of setters, getters, adders, etc..
    * 
    * @return string name of property for class
    * @todo make this a template method, duplicates override check from superclass
    */
    public function getProperty()
    {
        if( $this->hasPropertyOverride() )
        {
            return $this->property;
        }
        return $this->field;
    }
    
    /**
    * Setter strategy for Shuffler_Mapper->doLoad()
    * 
    * @param Shuffler_Model to inject data into
    * @return Shuffler_Model after injecting data
    */
    protected function doLoad( Shuffler_Model $model, array $row )
    {
        $value = $this->getRowField( $row, $this->getAliasedField() );   
        $command = $this->getMutatorMethod();
        if( method_exists( $model, $command ) || method_exists( $model, '__call' ) )
        {
            $model->$command( $value );
        }                              
        else
        {
            $property = $this->getProperty();
            $model->$property = $value;
        }
        return $model;
    }
    
    /**
    * Getter strategy for Shuffler_Mapper->getValues()
    * 
    * @param Shuffler_Model to get data from
    * @return return value from model's getter method
    */
    public function getFieldValue( Shuffler_Model $model )
    {
        $modelName = get_class( $model );
        $command = $this->getAccessorMethod();
        if( method_exists( $model, $command ) )
        {
            return $model->$command();
        }
        $property = $this->getProperty();
        return $model->$property;
    }
    
    public function getValue( $row )
    {
        return $this->getRowField( $row, $this->getTableName() . '_' . $this->getField() );
    }    
    
    public function getType()
    {
        return 'embedded';   
    }
    
    public function select( $select )
    {
        $db = $this->getReadAdapter();
        // if this field belongs on another table than the main table
        if( $this->table )
        { 
            /*$select->joinLeft(
                $this->table,
                $db->quoteIdentifier( $this->table ) . '.' . $db->quoteIdentifier( $this->getTableName() . '_id' ) . ' = ' . $this->getTableName() . '.id',
                array( $this->getTableName() . '_' . $this->field => $this->field )
            );*/
        }
        return $select;
    }
    
    function fieldIsInline()
    {
        $result = parent::fieldIsInline();
        if( $this->table )
        {
            $table = $this->getMapper()->getTableName();
            if( $table != $this->table )
            {
                $result = false;
            }
        }
        return $result;
    }
    
    public function getCommandSuffix()
    {
        if( $this->hasPropertyOverride() )
        {
            return $this->property;
        }
        $property = explode( '_', $this->field );
        foreach( $property as $k => $v )
        {
            $property[$k] = ucfirst( $v );
        }
        return implode( '', $property );
    } 
}